#region Using directives
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Threading;
#if UNIT_TESTING
//using NUnit.Framework;
#endif
#endregion

namespace MDXModelImporter
{
	/// <summary>
	/// Log will create automatically a log file and write
	/// log/error/debug info for simple runtime error checking, very useful
	/// for minor errors, such as finding not files.
	/// The application can still continue working, but this log provides
	/// an easy support to find out what files are missing (in this example).
	///
	/// Note: I don't use this class anymore for big projects, but its small
	/// and handy for smaller projects and nice to log non-debugable stuff.
	/// </summary>
	public class Log
	{
		#region Variables		
        private static bool isEnable;
        public static bool Enable
        {
            set { isEnable = value; }
        }
		// Writer		
		private static StreamWriter writer = null;		
		// Log filename		
		private const string LogFilename = "Log.txt";
		#endregion

		#region Static constructor to create log file
		/// <summary>
		/// Static constructor
		/// </summary>
		static Log()
		{
			try
			{                
				// Open file
                FileStream file = new FileStream(LogFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
				
				// Associate writer with that, when writing to a new file,
				// make sure UTF-8 sign is written, else don't write it again!
				writer = new StreamWriter(file,	System.Text.Encoding.UTF8);
				
				// Go to begin of file
				writer.BaseStream.Seek(0, SeekOrigin.Begin);
				// Enable auto flush (always be up to date when reading!)
				writer.AutoFlush = true;				
			} 
			catch
			{
				// Ignore any file exceptions, if file is not				
			} // catch
		} // Log()
		#endregion

		#region Write log entry
		/// <summary>
		/// Writes a LogType and info/error message string to the Log file
		/// </summary>
		static public void Write(string message)
		{
			// Can't continue without valid writer
			if (writer == null||isEnable==false)
				return;

			try
			{
                writer.WriteLine(message);
				//DateTime ct = DateTime.Now;
                //string s = "[" + ct.Hour.ToString("00") + ":" +
                //    ct.Minute.ToString("00") + ":" +
                //    ct.Second.ToString("00") + "] " +
                //    message;
				//writer.WriteLine(s);
#if DEBUG
				// In debug mode write that message to the console as well!
				//System.Console.WriteLine(s);
#endif
			} // try
			catch
			{
				// Ignore any file exceptions, if file is not
				// writable (e.g. on a CD-Rom) it doesn't matter
			} // catch
		} // Write(message)
		#endregion
	} // class Log
} // namespace XnaBreakout.Helpers
